<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;

use App\Admin\Models\AgencyLevel;
use App\Admin\Models\Common;

use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

class AgencyLevelController extends Controller
{
	use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('代理等级')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('代理等级')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('代理等级')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('代理等级')
            ->description('新增')
            ->body($this->form());
    }

//  public function update(Request $request, $id)
//  {
//
//  }

//  /**
//   * Handle update.
//   *
//   * @param int $id
//   *
//   * @return \Symfony\Component\HttpFoundation\Response
//   */
//  public function update($id, $data = null)
//  {
//
//      return $this->returnMsg(1, $id);
//  }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AgencyLevel);
        $grid->alId('ID');
        $grid->alName('等级名称');
        $grid->alLevel('等级');
        $grid->alMemo('备注');
        $grid->createTime('上传时间');

		// $grid->filter(function($filter){

		//     // 去掉默认的id过滤器
		//     $filter->disableIdFilter();

		// });

		$grid->actions(function ($actions) {
		    // $actions->disableDelete();
//		    $actions->disableEdit();
		    $actions->disableView();
		});

		// 默认展开筛选框
		// $grid->expandFilter();

		// 禁用行选择checkbox
		$grid->disableRowSelector();

		// 禁用导出
		$grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$Product = new Product();
        $show = new Show($Product->findOrFail($id));

        $show->sgId('ID');
        $show->sgName('产品名称');
        $show->sgLogo('产品图片');
        $show->sgDesc('产品描述');
        $show->sgPrice('产品单价');
        $show->dpId('分销规则');
		$Common = new Common();
		$dpSelect = $Common->getPreceptSelect();
        $show->dpId('分销规则')->using($dpSelect);
        $show->sgNum('库存');
        $show->sgWarningNum('库存警报数量');
        $show->createTime('创建时间');
        $show->updateUser('最后修改人');
        $show->updateTime('最后修改时间');
		$show->sgIsChaffer('议价')->using(['0' => '否', '1' => '是']);
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(AgencyLevel::class, function (Form $form) {

			$form->hidden('alId');
            $form->text('alName', '等级名称')->rules("required", ['required' => '请填写等级名称']);
            $form->number('alLevel', '等级')->rules("required", ['required' => '请填写等级'])->help('数字越大等级越大，每个等级不可重复');
            $form->text('alMemo', '备注');


			//$form->select('sId','店铺')->options('/store/getList');
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
			    // $tools->disableList();

			    // 去掉`删除`按钮
			    $tools->disableDelete();

			    // 去掉`查看`按钮
//			    $tools->disableView();

			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});

			$form->footer(function ($footer) {

			    // 去掉`重置`按钮
			    $footer->disableReset();

//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();

			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();

			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();

			    // 去掉`继续创建`checkbox
//			    $footer->disableCreatingCheck();

			});
        });
    }

}
